package com.gec.system.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class SendSms {

    public static boolean send(String phoneNumber,String code) {
        //AccessKey ID 指的是LTAI5tKTEwv1JJWDqC5dep5E
        //AccessKey Secret 指的是GdejWeHSb8eKChK9FWCT23VcpHaMli
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI5tKTEwv1JJWDqC5dep5E","GdejWeHSb8eKChK9FWCT23VcpHaMli");//AccessKey ID ,AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "强盛物业管理系统");//短信签名名称
        request.putQueryParameter("TemplateCode", "SMS_461985394");//短信模板id
        //如果是发送验证码格式就加下面这句 code就是你要发送的验证码
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");//code填充的参数（JSON对象格式)
//        {"code":"666666"}，即："{\"code\":\""+参数code+"\"}"
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String json = response.getData();
            Gson g = new Gson();
            HashMap result = g.fromJson(json, HashMap.class);
            if("OK".equals(result.get("Message"))) {
                return true;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}